define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'partner/stone/log/index' + location.search,
                    add_url: 'partner/stone/log/add',
                    edit_url: 'partner/stone/log/edit',
                    del_url: 'partner/stone/log/del',
                    multi_url: 'partner/stone/log/multi',
                    import_url: 'partner/stone/log/import',
                    table: 'partner_stone_log',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'from.username', title: __('From_id'), operate: 'LIKE'},
                        {field: 'to.username', title: __('To_id'), operate: 'LIKE'},
                        {field: 'stone_type', title: __('Stone_type'), searchList: {"1":__('Stone_type 1'),"2":__('Stone_type 2'),"3":__('Stone_type 3')}, formatter: Controller.api.formatter.stone},
                        {field: 'amount', title: __('Amount')},
                        {field: 'consumeinfo', title: __('Consumeinfo'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Controller.api.formatter.nullorautocontent},
                        {field: 'notice', title: __('Notice'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Controller.api.formatter.nullorautocontent},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        readonly: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'partner/stone/log/readonly' + location.search,
                    add_url: 'partner/stone/log/add',
                    edit_url: 'partner/stone/log/edit',
                    del_url: 'partner/stone/log/del',
                    multi_url: 'partner/stone/log/multi',
                    import_url: 'partner/stone/log/import',
                    table: 'partner_stone_log',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'frompartner.partner_account', title: __('From_id')},
                        {field: 'topartner.partner_account', title: __('To_id')},
                        {field: 'stone_type', title: __('Stone_type'), searchList: {"1":__('Stone_type 1'),"2":__('Stone_type 2'),"3":__('Stone_type 3')}, formatter: Controller.api.formatter.stone},
                        {field: 'amount', title: __('Amount')},
                        {field: 'consumeinfo', title: __('Consumeinfo'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Controller.api.formatter.nullorautocontent},
                        {field: 'notice', title: __('Notice'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Controller.api.formatter.nullorautocontent},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        consume: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'partner/stone/log/consume' + location.search,
                    add_url: 'partner/stone/log/add',
                    edit_url: 'partner/stone/log/edit',
                    del_url: 'partner/stone/log/del',
                    multi_url: 'partner/stone/log/multi',
                    import_url: 'partner/stone/log/import',
                    table: 'partner_stone_log',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {field: 'type', title: __('Type'), searchList: {"1":__('Type 1'),"2":__('Type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'stone_type', title: __('Stone_type'), searchList: {"1":__('Stone_type 1'),"2":__('Stone_type 2'),"3":__('Stone_type 3')}, formatter: Controller.api.formatter.stone},
                        {field: 'amount', title: __('Amount')},
                        {field: 'consumeinfo', title: __('Consumeinfo'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Controller.api.formatter.nullorautocontent},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                showdash: function (value, row, index) {
                    if (value) {
                        return value;
                    }
                    return '-';
                },
                nullorautocontent: function (value, row, index) {
                    if (value) {
                        var width = this.width != undefined ? (this.width.toString().match(/^\d+$/) ? this.width + "px" : this.width) : "250px";
                        var hover = this.hover != undefined && this.hover ? "autocontent-hover" : "";
                        return "<div class='autocontent-item " + hover + "' style='white-space: nowrap; text-overflow:ellipsis; overflow: hidden; max-width:" + width + ";'>" + value + "</div>";
                    }
                    return '-';
                },
                status: function (value, row, index) {
                    var colorArr = ["primary", "yellow", "success", "warning", "info", "gray", "red", "yellow", "aqua", "blue", "navy", "teal", "olive", "lime", "fuchsia", "purple", "maroon"];
                    var custom = {};
                    if (typeof this.custom !== 'undefined') {
                        custom = $.extend(custom, this.custom);
                    }
                    value = value == null || value.length === 0 ? '' : value.toString();
                    var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
                    var index = keys.indexOf(value);
                    var color = value && typeof custom[value] !== 'undefined' ? custom[value] : null;
                    var display = index > -1 ? this.searchList[value] : null;
                    var icon = typeof this.icon !== 'undefined' ? this.icon : null;
                    if (!color) {
                        color = index > -1 && typeof colorArr[index] !== 'undefined' ? colorArr[index] : 'primary';
                    }
                    if (!display) {
                        display = __(value.charAt(0).toUpperCase() + value.slice(1));
                    }
                    var html = '<span class="text-' + color + '">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';
                    if (this.operate != false) {
                        html = '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + this.field + '" data-value="' + value + '">' + html + '</a>';
                    }
                    return html;
                },

                stone: function (value, row, index) {
                    var colorArr = ["success", "blue", "red", "warning", "info", "gray", "red", "yellow", "aqua", "blue", "navy", "teal", "olive", "lime", "fuchsia", "purple", "maroon"];
                    var custom = {};
                    if (typeof this.custom !== 'undefined') {
                        custom = $.extend(custom, this.custom);
                    }
                    value = value == null || value.length === 0 ? '' : value.toString();
                    var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
                    var index = keys.indexOf(value);
                    var color = value && typeof custom[value] !== 'undefined' ? custom[value] : null;
                    var display = index > -1 ? this.searchList[value] : null;
                    var icon = typeof this.icon !== 'undefined' ? this.icon : null;
                    if (!color) {
                        color = index > -1 && typeof colorArr[index] !== 'undefined' ? colorArr[index] : 'primary';
                    }
                    if (!display) {
                        display = __(value.charAt(0).toUpperCase() + value.slice(1));
                    }
                    var html = '<span class="text-' + color + '">' + (icon ? '<i class="' + icon + '"></i> ' : '') + display + '</span>';
                    if (this.operate != false) {
                        html = '<a href="javascript:;" class="searchit" data-toggle="tooltip" title="' + __('Click to search %s', display) + '" data-field="' + this.field + '" data-value="' + value + '">' + html + '</a>';
                    }
                    return html;
                },
            }
        }
    };
    return Controller;
});
